/*!
 @header IntegralRecordsDetailViewController.m
 @author 周益
 @version 1.0.0 2013/02/26 Creation
 Copyright (c) 2013年 mypocketbaby. All rights reserved.
 */

#import "IntegralRecordsDetailViewController.h"
#import "Config.h"
#import "Helper.h"

//  -业务逻辑细节说明-
//
//  @abstract       InfoPickerViewController功能较为简单，通过自定义实例化方法- (id)initWithStyle:ByRecordId所设置的记录id，设置recordId，然后判断_record对象是否为空，
//                      如果为空，那么表示用户的操作流程是从消息通知后续操作push过来的，那么就根据前面的recordId向服务器请求制定的积分详情；否则就是通过积分记录push过来的，那么就根据传递过来的record字典对象设置相应的值。

//  @discussion
//
//

//  @notes          


//  -类修改日志-
//  修改人   修改时间   针对版本
//  修改描述


@interface IntegralRecordsDetailViewController ()

@end

@implementation IntegralRecordsDetailViewController

@synthesize record = _record;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithStyle:(UITableViewStyle)style ByRecordId:(NSInteger)recordId
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        
        _recordId = recordId;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = _L(@"vc_title_046");
    
    UIButton *backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated
{
    if (_record == nil) {
        
        //_record为空，则证明是从消息通知过来的
        [self recordsRequestById];
    }
    
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
        //详情类别label
        UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0f, 10.0f, 55.0f, 20.0f)];
        categoryLabel.font = _SYS_FONT_007;
        categoryLabel.textColor = _SYS_COLOR_002;
        [cell.contentView addSubview:categoryLabel];
        
        //详情内容label
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(categoryLabel.frame.origin.x + categoryLabel.frame.size.width + 20.0f, 10.0f, 250.0f, 20.0f)];
        contentLabel.font = _SYS_FONT_007;
        contentLabel.textColor = _SYS_COLOR_002;
        [cell.contentView addSubview:contentLabel];
        
    }
    
    //获得cell的子view
    NSArray *views = [cell.contentView subviews];
    
    //设置详情类别label
    UILabel *categoryLabel = [views objectAtIndex:0];
    
    //设置详情内容label
    UILabel *contentLabel = [views objectAtIndex:1];
    
    switch (indexPath.row) {
        case 0:
            categoryLabel.text = _L(@"title_16");   //时间：
            contentLabel.text = [_record objectForKey:@"createTime"];
            break;
        case 1:
            categoryLabel.text = _L(@"title_17");   //变更：
            contentLabel.text = [[_record valueForKey:@"action"] integerValue] != 0 ? @"减少" : @"增加";
            break;
        case 2:
            categoryLabel.text = _L(@"title_18");   //数量：
            contentLabel.text = [NSString stringWithFormat:@"%d", [[_record objectForKey:@"points"] integerValue]];
            break;
        case 3:
            categoryLabel.text = _L(@"title_19");   //原由：
            contentLabel.text = [_record objectForKey:@"originally"];
            break;
            
        default:
            break;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - 自定义事件

/*!
 @abstract 返回按钮事件
 */
- (void)backButtonEvent:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 私有方法

/*!
 @abstract 获得指定id的积分记录详情
 */
- (void)recordsRequestById
{
    JSONRequest *request = [[JSONRequest alloc] initWithURLString:_JSON_URL_ACCOUNT_SCORE];
    request.hudOn = YES;
    request.delegate = self;
    request.hudTitle = _L(@"title_loading_01");

    [request setValue:[NSString stringWithFormat:@"%d", _recordId] forParameter:@"scoreId"];
    [request asynchronous];
}

#pragma mark - JSONRequestDelegate

//
- (void)requestDidLoadJSON:(NSDictionary *)dict
{
    
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:dict];
    
    if (jmf.status == JMStatusSucceed) {
        
        _record = [dict objectForKey:@"data"];
        [self.tableView reloadData];
        
    }
    else {
        [Helper easyShowJMFStatus:jmf];
    }
}

- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:nil andMessage:_L(@"title_networkunavailabe")];
}
@end
